/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.geoviewer.jxmap.render;

import com.vividsolutions.jts.geom.Point;
import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.geom.Point2D;
import org.jdesktop.swingx.JXMapViewer;
import org.jdesktop.swingx.mapviewer.GeoPosition;

/**
 *
 * @author Timon Veenstra
 */
public class PointRenderer implements GeometryRenderer<Point> {

    private Font font = new Font("Dialog", Font.BOLD, 12);
    private double alpha = (100 - (double) 0 /*transparancy*/) / 100;
    private static int STROKE_WIDTH = 2; // stroke of the rectangle surrounding the label (pixels)

    @Override
    public Rectangle paint(Point point, JXMapViewer mapViewer, Graphics2D g, boolean selected) {
        Rectangle bounds = null;
        com.vividsolutions.jts.geom.Point centroid = point.getCentroid();
        GeoPosition centroidPos = new GeoPosition(centroid.getY(), centroid.getX());
        final Point2D pointOnMap = mapViewer.getTileFactory().geoToPixel(centroidPos, mapViewer.getZoom());

        Rectangle viewportBounds = mapViewer.getViewportBounds();

        if (viewportBounds.contains(pointOnMap)) {
            final int x = (int) (pointOnMap.getX() - viewportBounds.getX());
            final int y = (int) (pointOnMap.getY() - viewportBounds.getY());

            g.setFont(font);
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, new Float(alpha)));
            g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            g.setStroke(new BasicStroke(STROKE_WIDTH, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));

            Rectangle rect = new Rectangle(4, 4, 4, 4);
            rect.setLocation(x, y);
            g.fill(rect);
            bounds = rect;
        }
        return bounds;
    }

}
